import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

// createRouter 创建路由实例
// 配置 history 模式
// 1. history模式：createWebHistory     地址栏不带 #
// 2. hash模式：   createWebHashHistory 地址栏带 #
// console.log(import.meta.env.DEV)

// vite 中的环境变量 import.meta.env.BASE_URL  就是 vite.config.js 中的 base 配置项
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/login', component: () => import('@/views/Login/Register.vue') }, // 登录页
    {
      path: '/',
      component: () => import('@/views/Layout/Layout.vue'),
      redirect: 'accounts',
      meta: { requiresAuth: true },
      children: [
        // 管理员账户管理
        {
          path: 'accounts',
          meta: { requiresAuth: true, roles: ['super_admin'] },
          component: () => import('@/views/admin/AdminAccountsPage.vue'),
        },
        // 赛事与赛程管理
        {
          path: 'tournaments',
          meta: { requiresAuth: true, roles: ['super_admin', 'tournament_admin'] },
          component: () => import('@/views/tournaments/TournamentsPage.vue'),
        },
        // 队伍与选手管理
        {
          path: 'TeamPlayerManage',
          redirect: '/TeamsManage',
          meta: { requiresAuth: true, roles: ['super_admin', 'tournament_admin'] },
          children: [
            {
              path: '/TeamPlayerManage/TeamsManage',
              name: 'TeamsManage',
              component: () => import('@/views/teamsAndPlayer/TeamsPage.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
            {
              path: '/TeamPlayerManage/PlayersManage',
              name: 'PlayersManage',
              component: () => import('@/views/teamsAndPlayer/PlayersPage.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
          ],
        },

        //报名与审核管理
        {
          path: 'Review',
          redirect: '/TeamReview',
          meta: { requiresAuth: true, roles: ['super_admin', 'tournament_admin'] },
          children: [
            {
              path: '/Review/TeamReview',
              name: 'Teams',
              component: () => import('@/views/review/teamRegister.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
            {
              path: '/Review/autoMatch',
              name: 'Players',
              component: () => import('@/views/review/autoMatch.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
          ],
        },
        // 比赛管理
        {
          path: 'matches',
          meta: { requiresAuth: true, roles: ['super_admin', 'tournament_admin'] },
          component: () => import('@/views/matchs/MatchesPage.vue'),
        },
        // 数据统计与分析
        {
          path: 'statistics',
          name: 'Statistics',
          component: () => import('@/views/statistics/StatisticsPage.vue'),
          meta: { requiresAuth: true, roles: ['super_admin', 'data_admin'] },
        },
        // 系统设置与维护
        {
          path: 'settings',
          name: 'Settings',
          component: () => import('@/views/settings/SettingsPage.vue'),
          meta: { requiresAuth: true, roles: ['super_admin'] },
        },
        // 第三方服务集成
        {
          path: 'third-party',
          name: 'StreamingIntegration',
          component: () => import('@/views/thirdParty/StreamingIntegrationPage.vue'),
          meta: { requiresAuth: true, roles: ['super_admin'] },
        },
        {
          path: 'user',
          redirect: '/avator',
          meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
          children: [
            {
              path: '/user/avator',
              name: 'avator',
              component: () => import('@/views/User/AvatarPage.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
            {
              path: '/user/password',
              name: 'password',
              component: () => import('@/views/User/PasswordPage.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
            {
              path: '/user/profile',
              name: 'profile',
              component: () => import('@/views/User/ProfilePage.vue'),
              meta: { requiresAuth: true, roles: ['super_admin', 'team_admin'] },
            },
          ],
        },
      ],
    },
  ],
})

router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
})

export default router
